package org.timemanager.view;

import org.afree.chart.plot.PieLabelRecord;
import org.afree.chart.plot.PiePlot;
import org.afree.chart.plot.PiePlotState;
import org.afree.chart.text.G2TextMeasurer;
import org.afree.chart.text.TextBlock;
import org.afree.chart.text.TextBox;
import org.afree.chart.text.TextUtilities;
import org.afree.data.KeyedValues;
import org.afree.data.general.PieDataset;
import org.afree.graphics.PaintUtility;
import org.afree.graphics.geom.RectShape;
import org.afree.ui.HorizontalAlignment;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

public class MyPiePlot extends PiePlot {
	private boolean isLeafAlign=false;
	
	public MyPiePlot() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MyPiePlot(PieDataset dataset) {
		super(dataset);
		// TODO Auto-generated constructor stub
	}
	
	public MyPiePlot(PieDataset dataset,boolean bLeafAlign) {
		super(dataset);
		isLeafAlign=bLeafAlign;
	}

	@Override
	protected void drawLeftLabels(KeyedValues keys, Canvas canvas,
			RectShape plotArea, RectShape linkArea, float maxLabelWidth,
			PiePlotState state) {
		//super.drawLeftLabels(keys, canvas, plotArea, linkArea, maxLabelWidth, state);
		
        this.getLabelDistributor().clear();
        double lGap = plotArea.getWidth() * this.getLabelGap();
        double verticalLinkRadius = state.getLinkArea().getHeight() / 2.0;
        
        for (int i = 0; i < keys.getItemCount(); i++) {
            String label = this.getLabelGenerator().generateSectionLabel(this.getDataset(), keys.getKey(i));
            if (label != null) {
            	Paint paint = PaintUtility.createPaint(Paint.ANTI_ALIAS_FLAG, this.getLabelPaintType(),
            			this.getLabelFont());
                TextBlock block = TextUtilities.createTextBlock(label, this.getLabelFont(),
                        this.getLabelPaintType(), maxLabelWidth, new G2TextMeasurer(paint));
                if(isLeafAlign){
                	block.setLineAlignment(HorizontalAlignment.LEFT);
                }
                TextBox labelBox = new TextBox(block);
                labelBox.setBackgroundPaintType(this.getLabelBackgroundPaintType());
                labelBox.setOutlinePaintType(this.getLabelOutlinePaintType());
                labelBox.setOutlineStroke(this.getLabelOutlineStroke());
                labelBox.setShadowPaintType(this.getLabelShadowPaintType());
                labelBox.setInteriorGap(this.getLabelPadding());
                double theta = Math.toRadians(keys.getValue(i).doubleValue());
                double baseY = state.getPieCenterY() + Math.sin(theta) * verticalLinkRadius;
                double hh = labelBox.getHeight(canvas);

                this.getLabelDistributor().addPieLabelRecord(new PieLabelRecord(keys.getKey(i), theta,
                        baseY, labelBox, hh, lGap / 2.0 + lGap / 2.0 * -Math.cos(theta), 1.0
                                - getLabelLinkDepth() + getExplodePercent(keys.getKey(i))));
            }
        }
        double hh = plotArea.getHeight();
        double gap = hh * getInteriorGap();
        this.getLabelDistributor().distributeLabels(plotArea.getMinY() + gap, hh - 2 * gap);
        for (int i = 0; i < this.getLabelDistributor().getItemCount(); i++) {
            drawLeftLabel(canvas, state, this.getLabelDistributor().getPieLabelRecord(i));
        }
	}

	@Override
	protected void drawRightLabels(KeyedValues keys, Canvas canvas,
			RectShape plotArea, RectShape linkArea, float maxLabelWidth,
			PiePlotState state) {
		//super.drawRightLabels(keys, canvas, plotArea, linkArea, maxLabelWidth, state);
		
		this.getLabelDistributor().clear();
        double lGap = plotArea.getWidth() * this.getLabelGap();
        double verticalLinkRadius = state.getLinkArea().getHeight() / 2.0;

        for (int i = 0; i < keys.getItemCount(); i++) {
            String label = this.getLabelGenerator().generateSectionLabel(this.getDataset(), keys.getKey(i));

            if (label != null) {
                Paint paint = PaintUtility.createPaint(Paint.ANTI_ALIAS_FLAG,
                        this.getLabelPaintType(), this.getLabelFont());

                TextBlock block = TextUtilities.createTextBlock(label, this.getLabelFont(),
                        this.getLabelPaintType(), maxLabelWidth, new G2TextMeasurer(paint));
                if(isLeafAlign){
                	block.setLineAlignment(HorizontalAlignment.LEFT);
                }
                TextBox labelBox = new TextBox(block);
                labelBox.setBackgroundPaintType(this.getLabelBackgroundPaintType());
                labelBox.setOutlinePaintType(this.getLabelOutlinePaintType());
                labelBox.setOutlineStroke(this.getLabelOutlineStroke());
                labelBox.setShadowPaintType(this.getLabelShadowPaintType());
                labelBox.setInteriorGap(this.getLabelPadding());
                double theta = Math.toRadians(keys.getValue(i).doubleValue());
                double baseY = state.getPieCenterY() + Math.sin(theta) * verticalLinkRadius;
                double hh = labelBox.getHeight(canvas);
                this.getLabelDistributor().addPieLabelRecord(new PieLabelRecord(keys.getKey(i), theta,
                        baseY, labelBox, hh, lGap / 2.0 + lGap / 2.0 * Math.cos(theta), 1.0
                                - getLabelLinkDepth() + getExplodePercent(keys.getKey(i))));
            }
        }
        double hh = plotArea.getHeight();
        double gap = hh * getInteriorGap();
        this.getLabelDistributor().distributeLabels(plotArea.getMinY() + gap, hh - 2 * gap);
        for (int i = 0; i < this.getLabelDistributor().getItemCount(); i++) {
            drawRightLabel(canvas, state, this.getLabelDistributor().getPieLabelRecord(i));
        }
	}

}
